package com.resou.reader.utils;

import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String getAllHour(long millisecond) {
        long l = millisecond % (1000 * 60 * 60 * 24) / (1000 * 60 * 60) + 24 * getDay(millisecond);
        return String.valueOf(l);
    }

    public static long getDay(long millisecond) {
        return millisecond / (1000 * 60 * 60 * 24);
    }

    public static long getHour(long millisecond) {
        return millisecond % (1000 * 60 * 60 * 24) / (1000 * 60 * 60);
    }

    public static long getMinute(long millisecond) {
        return millisecond % (1000 * 60 * 60) / (1000 * 60);
    }

    public static long getSecond(long millisecond) {
        return millisecond % (1000 * 60) / 1000;
    }

    /**
     * 根据毫秒数获取天字符串
     *
     * @param millisecond 毫秒数
     * @return 毫秒数的天字符串
     */
    public static String getStringDay(long millisecond) {
        long day = millisecond / (1000 * 60 * 60 * 24);
        return String.valueOf(day);
    }

    /**
     * 根据毫秒数获取时字符串
     *
     * @param millisecond 毫秒数
     * @return 毫秒数的时字符串
     */
    public static String getStringHour(long millisecond) {
        long hour = millisecond % (1000 * 60 * 60 * 24) / (1000 * 60 * 60);
        return String.valueOf(hour);
    }


    /**
     * 根据毫秒数获取分字符串
     *
     * @param millisecond 毫秒数
     * @return 毫秒数的分字符串
     */
    public static String getStringMinute(long millisecond) {
        long minute = millisecond % (1000 * 60 * 60) / (1000 * 60);
        return String.valueOf(minute);
    }

    /**
     * 根据毫秒数获取秒字符串
     *
     * @param millisecond 毫秒数
     * @return 毫秒数的秒字符串
     */
    public static String getStringSecond(long millisecond) {
        long second = millisecond % (1000 * 60) / 1000;
        return String.valueOf(second);
    }


    /**
     * 将毫秒转化为 分钟：秒 的格式
     *
     * @param millisecond 毫秒
     * @return
     */
    public String formatTime(long millisecond) {
        int minute;//分钟
        int second;//秒数
        int hour;   //小时

        minute = (int) ((millisecond / 1000) / 60);
        second = (int) ((millisecond / 1000) % 60);
        if (minute < 10) {
            if (second < 10) {
                return "0" + minute + ":" + "0" + second;
            } else {
                return "0" + minute + ":" + second;
            }
        } else {
            if (second < 10) {
                return minute + ":" + "0" + second;
            } else {
                return minute + ":" + second;
            }
        }
    }
}
